﻿using IBLL;
using IDAL;
using Model;

namespace BLL
{
    public class ShopBLL : IShopBLL
    {
        public ShopBLL(IShopDAL<ShopModel>dal,IShopDAL<PlaceModel>pal)
        {
            Dal = dal;
            Pal = pal;
        }

        public IShopDAL<ShopModel> Dal { get; }
        public IShopDAL<PlaceModel> Pal { get; }

        public async Task<int> AddShop(ShopModel shop)
        {
            return await Dal.AddShopAsync(shop);
        }

        public Task<int> DeleteShop(int id)
        {
            var shop=Dal.GetAllShopAsync().FirstOrDefault(a=>a.Id==id);
            return Dal.DeleteShopAsync(shop);
        }

        public dynamic GetShopShow(string? name, int? pid, DateTime? dateTime, int state)
        {
            var a = Dal.GetAllShopAsync().Where(a=>a.IsDeleted==true);
            var b=Pal.GetAllShopAsync();
            var sql = from c in a
                      join d in b on c.Pid equals d.Pid
                      select new
                      {
                          c.Id,
                          c.ShopName,
                          c.Pid,
                          c.State,
                          c.ShopBoss,
                          c.Phone,
                          c.CreateTime,
                          c.MaxPeople,
                          d.PlaceName
                      };
            if (!string.IsNullOrEmpty(name)) 
            {
                sql = sql.Where(a => a.ShopBoss.Contains(name));
            }
            if (pid > 0) 
            {
                sql=sql.Where(a => a.Pid == pid);
            }
            if (dateTime != null) 
            {
                var s=dateTime.Value.AddDays(1);
                sql = sql.Where(a => a.CreateTime >= dateTime && a.CreateTime < s);
            }
            if (state > 0) 
            {
                sql = sql.Where(a => a.State == state);
            }
            return sql;
        }

        public Task<int> UpdateShop(ShopModel shop)
        {
            return Dal.UpdateShopAsync(shop);
        }
    }
}
